package utils;

public class OOBB {

	public static final int NOTHING = 0, HOLD = 1, STARTOVER = 2, REVERSE = 3;

	/**
	 * Transforms the index <code>i</code> using <code>outOfBoundsBehavior</code> to determine what to return if <code>i</code> is out of bounds. The bounds are from (
	 * <code>0</code> to <code>length - 1</code>)
	 **/
	public static int transform(int n, int length, int outOfBoundsBehavior) {
		int i = n, j;
		switch (outOfBoundsBehavior) {
		case OOBB.NOTHING:
			if (i < 0 || i >= length)
				return -1;
			return i;
		case OOBB.HOLD:
			if (i < 0)
				return 0;
			if (i >= length)
				return length - 1;
			return i;
		case OOBB.STARTOVER:
			j = i % length;
			return j + ((j < 0) ? length : 0);
		case OOBB.REVERSE:
			length--;
			i = ((i < 0) ? -i : i);
			j = i % (length * 2);
			if (j > length)
				i = (-i) % (length * 2);
			if (j != length)
				j = i % length;
			return j + ((j < 0) ? length : 0);
		}
		return -1;
	}

// TEST
//	public static void main(String[] args) {
//		for (int i = -10; i < 10; i++)
//			System.out.println(i + "\t" + OOBB.transform(i, 3, OOBB.REVERSE));
//	}
}
